/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.tapestry.view.components;

import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IActionListener;
import org.apache.tapestry.IAsset;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.Asset;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.listener.ListenerInvoker;

public abstract class EditableHTMLContent extends BaseComponent{

    @Asset("/images/edit.gif")
    public abstract IAsset getEditImage(); 
    
    @Parameter(required=true)
    public abstract String getValue();
    public abstract void setValue(String value);
    
    @Parameter(required=false, defaultValue="15")
    public abstract String getRows();
    public abstract void setRows(String value);
    
    @Parameter(required=false, defaultValue="80")
    public abstract String getCols();
    public abstract void setCols(String value);

    @Parameter(required=false, defaultValue ="null")
    public abstract TinyMCEConfigurationFactory getConfigurationFactory();
    public abstract void setConfigurationFactory(TinyMCEConfigurationFactory value);
    
    @Parameter(required=true)
    public abstract boolean isEditable();
    public abstract void setEditable(boolean v);
    
    @Parameter(required=true)
    public abstract boolean isEditing();
    public abstract void setEditing(boolean v);
    
    @Parameter(required = true)
    public abstract IActionListener getSaveListener();
    
    @Parameter(required = false, defaultValue="null")
    public abstract IActionListener getEditListener();
    
    @Parameter(required=false, defaultValue="'editTool'")
    public abstract String getEditToolClass();
    public abstract void setEditToolClass(String c);
    
    @Parameter(required=false, defaultValue="null")
    public abstract Object getParameters();
    public abstract void setParameters(Object o);
    
    @InjectObject("infrastructure:listenerInvoker")
    public abstract ListenerInvoker getListenerInvoker();
    
    public void editSubmit(IRequestCycle cycle)
    {
        if(getEditListener() != null)
        {
            getListenerInvoker().invokeListener(getEditListener(), this, cycle);
        }
        setEditing(true);
    }
    
    public void cancelSubmit(IRequestCycle cycle)
    {
        setEditing(false);
        //todo:redirect
    }
    
    public void saveSubmit(IRequestCycle cycle)
    {
        getListenerInvoker().invokeListener(getSaveListener(), this, cycle);

        setEditing(false);
        //todo:redirect
    }
    
    public void saveAndContinueSubmit(IRequestCycle cycle)
    {
        getListenerInvoker().invokeListener(getSaveListener(), this, cycle);
    }
}
